package com.quectel.cms.controller.banner;

import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.core.mobilebanner.MobileBannerConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.banner.dto.BannerDto;
import com.quectel.core.module.banner.service.BannerService;
import com.quectel.core.module.banner.service.MobileBannerVillageService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-16 09:11:29
 */
@RestController
@RequestMapping("banner")
@Tag(name = "BannerController", description = "移动端banne资源管理相关api")
public class BannerController extends BaseController {

    @DubboReference
    private BannerService bannerService;
    @DubboReference
    private MobileBannerVillageService mobileBannerVillageService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "banner:list")
    @Operation(summary = "移动端banner资源的分页查询列表")
    public Response<Page<BannerDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = bannerService.queryTotal(request.getParams());

        List<BannerDto> list = null;
        if (total > 0) {
            list = bannerService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<BannerDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<BannerDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "banner:info")
    @Operation(summary = "查询单条移动端banner资源记录")
    public Response<BannerDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (bannerService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        BannerDto bannerDto = bannerService.selectById(id);
        if (MobileBannerConstants.BINDING_VILLAGE_PART.equals(bannerDto.getIsBindingAll())) {
            List<VillageDto> villageDtoList = mobileBannerVillageService.selectByMobileBannerId(bannerDto.getId());
            bannerDto.setVillageDtoList(villageDtoList);
        }

        return Response.<BannerDto>ok().wrap(bannerDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "banner:edit")
    @Operation(summary = "保存单条移动端banner资源记录")
    public Response<Object> save(@RequestBody @Validated BannerDto bannerDto) {

        //填充基础数据
        fillSaveCommonData(bannerDto);

        if (MobileBannerConstants.BINDING_VILLAGE_ALL.equals(bannerDto.getIsBindingAll())) {
            //绑定全部小区
            if (!SysConstants.SYS_TOP_GROUP_CODE.equals(SessionHolder.getSysUser().getGroupCode())) {
                //只有超级组才能
                return Response.error("您的权限不能设置关联所有小区,请关联部分小区");
            }
        }

        bannerService.saveOrUpdate(bannerDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "banner:edit")
    @Operation(summary = "修改单条移动端banner资源记录")
    public Response<Object> update(@RequestBody @Validated BannerDto bannerDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(bannerDto.getId()));
        if (bannerService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(bannerDto);

        if (MobileBannerConstants.BINDING_VILLAGE_ALL.equals(bannerDto.getIsBindingAll())) {
            //绑定全部小区
            if (!SysConstants.SYS_TOP_GROUP_CODE.equals(SessionHolder.getSysUser().getGroupCode())) {
                //只有超级组才能
                return Response.error("您的权限不能设置关联所有小区,请关联部分小区");
            }
        }

        bannerService.saveOrUpdate(bannerDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "banner:delete")
    @Operation(summary = "批量删除存在的移动端banner资源记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (bannerService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        bannerService.deleteBatch(ids);

        return Response.ok();
    }
}
